package logic

import (
	"cloud-disk/core/define"
	"cloud-disk/core/utils"
	"context"

	"cloud-disk/core/internal/svc"
	"cloud-disk/core/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RefreshAuthorizationLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRefreshAuthorizationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RefreshAuthorizationLogic {
	return &RefreshAuthorizationLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RefreshAuthorizationLogic) RefreshAuthorization(req *types.RefreshAuthorizationRequest, Token string) (resp *types.RefreshAuthorizationResponse, err error) {
	claims, err := utils.ParseToken(Token)
	if err != nil {
		return
	}
	token, err := utils.GenToken(claims.Id, claims.Identity, claims.Name, define.TokenExpires)
	if err != nil {
		return
	}
	refreshToken, err := utils.GenToken(claims.Id, claims.Identity, claims.Name, define.RefreshTokenExpires)
	if err != nil {
		return
	}
	resp = &types.RefreshAuthorizationResponse{}
	resp.RefreshToken = refreshToken
	resp.Token = token
	return
}
